package builtin

import (
	"talent.com/agency_im/common/msg"
	"talent.com/agency_im/common/user"
	"talent.com/agency_im/conf"
	"talent.com/agency_im/conf/g"
	"talent.com/server/durotar.git/log"
	"talent.com/server/durotar.git/tools/tz"
)

type UserLeavingInterceptor struct {
}

func (b *UserLeavingInterceptor) PreHandle(message *msg.Message) (bool, error) {
	var (
		u   *user.User
		err error
	)
	log.Debug("pre handle UserLeavingInterceptor, message: %+v", message)
	if u, err = user.GetUser(message.SourceID); err != nil {
		log.Debug("pre handle UserLeavingInterceptor fail %v", err)

	} else {
		if u.PlatformID == conf.ServerConfig().Pay.AgencyPlatformID {
			g.UserLeavingTimeMap.Delete(u.ID)
		} else {
			if _, ok := g.UserLeavingTimeMap.Load(message.TargetID); !ok {
				g.UserLeavingTimeMap.Store(message.TargetID, uint64(tz.GetNowTs()))
			}
		}
	}
	return true, nil
}

func (b *UserLeavingInterceptor) PostHandle(message *msg.Message) error {
	return nil
}
